﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NS.Utilities;
using Filter;
using Newtonsoft.Json.Linq;
using HttpUtility;
using WechatModel;

namespace WechatDAL
{
    public class W_UserDal
    {
        /// <summary>
        /// 创建成员
        /// </summary>
        /// <param name="m">实体对象</param>
        /// <returns></returns>
        public bool Insert(W_UserInfo m)
        {
            JObject jo = Poster.Post(WechatAPI4Ent.AddUser, JsonHelper.T2JSON(m));
            if (jo == null)
                return false;
            else
            {
                if (jo["errcode"].ToString() == "0" && jo["errmsg"].ToString() == "created")
                    return true;
                else
                    return true;
            }
        }

        /// <summary>
        /// 修改成员
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool Update(W_UserInfo m)
        {
            JObject jo = Poster.Post(WechatAPI4Ent.UpdateUser, JsonHelper.T2JSON(m));
            if (jo == null)
                return false;
            else
            {
                if (jo["errcode"].ToString() == "0" && jo["errmsg"].ToString() == "updated")
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// 删除人员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            JObject jo = Poster.Get(WechatAPI4Ent.DeleteUser, id.ToString());
            if (jo == null)
                return false;
            else
            {
                if (jo["errcode"].ToString() == "0" && jo["errmsg"].ToString() == "deleted")
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// 邀请成员关注企业号
        /// </summary>
        /// <param name="id">成员ID</param>
        /// <param name="msg">提示消息</param>
        /// <returns></returns>
        public bool Invite(int id, string msg)
        {
            JObject jo = Poster.Post(WechatAPI4Ent.InviteUser, "{\"userid\":\"" + id.ToString() + "\",\"invite_tips\":\"" + msg + "\"}");
            if (jo == null)
                return false;
            else
            {
                if (jo["errcode"].ToString() == "0" && jo["errmsg"].ToString() == "ok")
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// 获取部门成员（复杂信息）
        /// </summary>
        /// <param name="dptid">部门信息</param>
        /// <returns></returns>
        public List<W_UserInfo> GetUsers(int dptid, int status)
        {
            //&department_id=&fetch_child=&status=
            JObject jo = Poster.Get(WechatAPI4Ent.GetUserInfosByDepartment, "&department_id=" + dptid.ToString() + "&fetch_child=1&status=" + status.ToString());
            if (jo == null)
                return null;
            else
            {
                if (jo["errcode"].ToString() == "0" && jo["errmsg"].ToString() == "ok")
                {
                    List<W_UserInfo> users = JsonHelper.JSON2T<List<W_UserInfo>>(jo["userlist"].ToString());
                    return users;
                }
                else
                    return null;
            }
        }
    }
}
